﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Commands;
using Microsoft.Practices.CompositeUI.EventBroker;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;
using SCSFContrib.CompositeUI.WPF.Workspaces;
using WPFCABShell.Shell.Interface;
using WPFCABShell.Shell.Interface.Services.Menu;
using WPFCABShell.Shell.Interface.Services.StatusBar;
using WPFCABShell.Shell.Options;

namespace WPFCABShell.Shell
{
    /// <summary>
    /// Implements the main window of the application.
    /// </summary>
    public partial class MainWindow : Window, IMainWindow
    {
		private WorkItem _workItem;

		public static readonly DependencyProperty PresenterProperty = DependencyProperty.Register("Presenter",
			typeof(MainWindowPresenter),
			typeof(MainWindow),
			new FrameworkPropertyMetadata(Presenter_Changed));

		[CreateNew]
		public MainWindowPresenter Presenter
		{
			get
			{
				return GetValue(PresenterProperty) as MainWindowPresenter;
			}
			set
			{
				SetValue(PresenterProperty, value);
			}
		}

		[Dependency]
		public WorkItem WorkItem
		{
			get
			{
				return _workItem;
			}
			set
			{
				_workItem = value;
			}
		}

		internal object MainMenu
		{
			get
			{
				return _menu;
			}
		}

		internal object ToolBarTray
		{
			get
			{
				return _toolBarTray;
			}
		}

		internal object ToolBarsContextMenu
		{
			get
			{
				return Resources["ToolBarsContextMenu"];
			}
		}

		internal IWorkspace MainWorkspace
		{
			get
			{
				return _mainWorkspace;
			}
		}

        public MainWindow()
        {
            InitializeComponent();
			_progressBarStatusBarItem.DataContext = ProgressEventArgs.Empty;
		}

		[CommandHandler(Commands.Exit)]
		public void Exit(object sender, EventArgs e)
		{
			Close();
		}

		[EventSubscription(Events.MessageChanged, Thread = ThreadOption.UserInterface)]
		public void MessageChanged(object sender, MessageEventArgs e)
		{
			_primaryMessageStatusBarItem.DataContext = e;
		}

		[EventSubscription(Events.SecondaryMessageChanged, Thread = ThreadOption.UserInterface)]
		public void SecondaryMessageChanged(object sender, MessageEventArgs e)
		{
			_secondaryMessageStatusBarItem.DataContext = e;
		}

		[EventSubscription(Events.TernaryMessageChanged, Thread = ThreadOption.UserInterface)]
		public void TernaryMessageChanged(object sender, MessageEventArgs e)
		{
			_ternaryMessageStatusBarItem.DataContext = e;
		}

		[EventSubscription(Events.ProgressChanged, Thread = ThreadOption.UserInterface)]
		public void ProgressChanged(object sender, ProgressEventArgs e)
		{
			_progressBarStatusBarItem.DataContext = null;
			_progressBarStatusBarItem.DataContext = e;
		}

		private static void Presenter_Changed(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
		{
			Debug.Assert(dependencyObject is MainWindow);

			if (e.NewValue != null)
			{
				Debug.Assert(e.NewValue is MainWindowPresenter);
				(e.NewValue as MainWindowPresenter).View = dependencyObject as MainWindow;
			}
		}
	}
}
